#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "yonk.h"


void user_help(void);
void quit(void);


int main(int argc, char **argv)
{
	int sockfd;
	if(0 > (sockfd = socket(AF_INET, SOCK_STREAM, 0))){
		perror("socket");
		return -1;
	}

	struct sockaddr_in serveraddr;
	bzero(&serveraddr, sizeof(serveraddr));
	serveraddr.sin_family 		= AF_INET;
	serveraddr.sin_port 		= atoi(argv[2]);
	serveraddr.sin_addr.s_addr 	= inet_addr(argv[1]);

	if(0 > connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))){
		perror("connect");
		return -1;
	}

	USER user;
	char cache[N];
	while(1){	
		bzero(cache, sizeof(cache));
		bzero(&user, sizeof(user));
		puts("*****Login first or Enter 'help',please!*****");
		puts("---------------------------------------------");
		puts("please Input user name or Enter 'help'");
		printf(".oO>> ");
		if(NULL == fgets(cache, sizeof(cache), stdin)){
			perror("fgets");
			continue;
		}
		if(5 > strlen(cache)){
			printf("arguments error,try again Please!\n");
			continue;
		}
		cache[strlen(cache) - 1] = '\0';
//		puts(cache);
		if(!strcmp(cache, "help"))
			user_help();
		if(!strcmp(cache, "quit"))
			quit();
		else{
			strcpy(user.name, cache);
			printf(".oO>> ");
			if(NULL == fgets(user.password, sizeof(user.password), stdin)){
				perror("fgets");
				fprintf(stderr,"input error,please try again!\n");
				continue;
			}
			user.password[strlen(user.password) - 1] = '\0';
//			puts(user.password);
			if(0 > login(sockfd, &user))
				continue;
			else
				break;
		}
	}

	puts("----------------Login sucess!-------------------");
	int option;
	while(1){
		if(1 == user.type){
			puts("please Enter a number to choice:");
			puts("1:--------<append>");
			puts("2:--------<delete>");
			puts("3:--------<modification>");
			puts("4:--------<acquire>");
			puts("5:--------<quit>");

			printf(".oO>> ");
			scanf("%d",&option);
			getchar();
			if(option > 5 || option < 1){
				printf("Enter argument error,Refence to the arguments carefully Please!\n");
				continue;
			}

			if(0 > send(sockfd, &option, sizeof(option), 0)){
				perror("send");
				continue;
			}
			
			switch(option){
				case 1:
					if(0 > append(sockfd)){
						fprintf(stderr, "append error,try and again Please!\n");
					}
					break;
				case 2:
					if(0 > delete(sockfd)){
						fprintf(stderr, "delete error,try and again Please!\n");
					}
					break;
				case 3:
					if(0 > modification(sockfd, &user)){
						fprintf(stderr, "modification error,try and again Please!\n");
					}
					break;
				case 4:
					if(0 > acquire(sockfd, &user)){
						fprintf(stderr, "modification error,try and again Please!\n");
					}
					break;
				case 5:
					quit();
					break;	
			}
		}
		else if(0 == user.type){
			puts("please Enter a number to choice:");
			puts("1:--------<modification>");
			puts("2:--------<acquire>");
			puts("3:--------<quit>");

			printf(".oO>> ");
			scanf("%d",&option);
			if(option > 3 || option < 1){
				printf("Enter argument error,Refence to the arguments carefully Please!\n");
				continue;
			}

			if(0 > send(sockfd, &option, sizeof(option), 0)){
				perror("send");
				continue;
			}

			switch(option){
				case 1:
					if(0 > modification(sockfd, &user)){
						fprintf(stderr, "modification error,try and again Please!\n");
					}
					
					break;
				case 2:
					if(0 > acquire(sockfd, &user)){
						fprintf(stderr, "modification error,try and again Please!\n");
					}
					break;
				case 3:
					quit();
					break;
			}
		}
	}
	
	return 0;
}

void user_help(void)
{
	puts("All of user must login first,and then do other operations");
	puts("***************General user:");
	puts("<acquire>:acquire the infomation of yourself account!");
	puts("<modification>:change the infomation of account!");
	puts("<exit>:exit the account!");
	
	puts("***************Administration user:");
	puts("<acquire>:acquire the infomation of yourself account!");
	puts("<modification>:change the infomation of account!");
	puts("<exit>:exit the account!");
	puts("<append>:add a user account!");
	puts("<delete>:delete a user account!");
}

int login(int sockfd, USER *user)
{
	if(0 > send(sockfd, user, sizeof(USER), 0)){
		fprintf(stderr,"%d -> %s: send false!\n",__LINE__,__func__);
		return -1;
	}

	COMUNI comuni;
	bzero(&comuni, sizeof(comuni));
	if(0 > recv(sockfd, &comuni, sizeof(comuni), 0)){
		fprintf(stderr,"%d -> %s: recv false!\n",__LINE__,__func__);
		return -1;
	}

	printf("ACK:%d\n",comuni.login_ack);
	
	switch(comuni.login_ack){
		case 0:						//0:all right

			if(0 > recv(sockfd, &user->type, sizeof(user->type), 0)){
				fprintf(stderr,"%d -> %s: recv false!\n",__LINE__,__func__);
				return -1;
			}
			printf("type:%d\n",user->type);
			printf("account:%s login success!\n",user->name);
			break;
			
		case 1:						//1:account not exists
			printf("account:%s not exists!\n",user->name);			
			break;

		case 2:						//2:password error
			printf("account:%s password error!\n",user->name);
			break;

		case 3:						//3:already login
			printf("account:%s already login!\n",user->name);
			break;

		case 4:						//4:server unusual
			printf("account:%s server unusual!\n",user->name);
			break;
	}

	if(0 != comuni.login_ack){
		return -1;
	}

	return 0;
}


int append(int sockfd)
{
	INFO info;
	int i;
	bzero(&info, sizeof(info));
	while(1){
		puts("Please Enter name:");
		printf(".oO>> ");
		if(NULL == fgets(info.name, sizeof(info.name), stdin)){
			perror("fgets");
			return -1;
		}
		info.name[strlen(info.name) - 1] = '\0';
		if(4 > strlen(info.name) || 20 < strlen(info.name)){
			puts("name length incorrect(betwen 4 ~ 20 character),try again Please!");
			continue;
		} 

		printf("***name: %s, length:%d\n",info.name, strlen(info.name));
		
		i = 0;
		while (i < strlen(info.name)){
			if(info.name[i] == ' '){
				puts("space key is not character permited, try again Please!");
				break;
			}
			i++;
		}

		if(i == strlen(info.name))
			break;
	}

	while(1){
		puts("Please Enter addr:");
		printf(".oO>> ");
		if(NULL == fgets(info.addr, sizeof(info.addr), stdin)){
			perror("fgets");
			return -1;
		}
		info.addr[strlen(info.addr) - 1] = '\0';
		if(4 > strlen(info.addr) || 50 < strlen(info.addr)){
			puts("addr length incorrect(betwen 4 ~ 50 character),try again Please!");
			continue;
		}

		printf("***addr: %s, length:%d\n",info.addr, strlen(info.addr));

		
		i =0;
		while (i < strlen(info.addr)){
			if(info.addr[i] == ' '){
				puts("space key is not character permited, try again Please!");
				break;
			}
			i++;
		}
		if(i == strlen(info.addr))
			break;
	}
	
	while(1){
		puts("Please Enter phone:");
		printf(".oO>> ");
		if(NULL == fgets(info.phone, sizeof(info.phone), stdin)){
			perror("fgets");
			return -1;
		}
		info.phone[strlen(info.phone) - 1] = '\0';
		if(11 != strlen(info.phone) && 8 != strlen(info.phone)){
			puts("phone length incorrect(8 or 11 character),try again Please!");
			continue;
		}

		printf("***phone: %s, length:%d\n",info.phone, strlen(info.phone));

		i = 0;
		while (i < strlen(info.phone)){
			if(info.phone[i] == ' '){
				puts("space key is not character permited, try again Please!");
				break;
			}
			i++;
		}

		if(i == strlen(info.phone))
			break;
	}

	while(1){
		puts("Please Enter age:(during 18 ~ 99)");
		printf(".oO>> ");
		scanf("%d",&info.age);
		if(info.age < 18 || info.age > 99){
			printf("Please input reality age!\n");
			continue;
		}else
			break;
	}
	while(1){
		puts("Please Enter level:(during 0 ~ 7)");
		printf(".oO>> ");
		scanf("%d",&info.level);
		if(info.level < 0 || info.level > 7){
			printf("Please input reality level!\n");
			continue;
		}else
			break;

	}
	while(1){
		puts("Please Enter empnum:(during 1000 ~ 999999)");
		printf(".oO>> ");
		scanf("%d",&info.empnum);
		if(info.empnum < 1000 || info.empnum> 999999){
			printf("Please input right empnum!\n");
			continue;
		}else
			break;

	}
	while(1){
		puts("Please Enter salary:(during 1000 ~ 1000000)");
		printf(".oO>> ");
		scanf("%lf",&info.salary);
		if(info.salary < 1000 || info.salary > 1000000){
			printf("Please input reality salary!\n");
			continue;
		}else
			break;

	}
	
	puts("write over!");
	
	if(0 > send(sockfd, &info, sizeof(info), 0)){
		perror("send");
		return -1;
	}

	printf("%d -> %s: send employee infomations success!\n",__LINE__,__func__);

	COMUNI comuni;
	bzero(&comuni, sizeof(comuni));
	if(0 > recv(sockfd, &comuni, sizeof(comuni), 0)){
		perror("recv");
		return -1;
	}

	if(10 == comuni.choice){
		fprintf(stderr,"%d -> %s: append false!\n",__LINE__,__func__);
		return -1;
	}else if(12 == comuni.choice){
		fprintf(stderr,"employee number have exists!\n");
		return -1;		
	}else if(0 == comuni.login_ack){
		puts("************--->>>  Login  false  <<<---****************");
		puts("************--->>> append success <<<---****************");
	}else if(1 == comuni.login_ack){
		puts("************--->>>  Login success <<<---****************");
		puts("************--->>> append success <<<---****************");
	}

	return 0;
}

int acquire(int sockfd, USER *user)
{
	TRAN_INFO info;
	COMUNI comuni;
	bzero(&comuni, sizeof(comuni));

	if(0 == user->type){
		bzero(&info, sizeof(info));
		if(0 > send(sockfd, user, sizeof(*user), 0)){
			perror("send");
			return -1;
		}

		if(0 > recv(sockfd, &info, sizeof(info), 0)){
			perror("recv");
			return -1;
		}

		puts("********************************************************");
		puts("*                Personal Information                  *");
		puts("********************************************************\n");
		printf("->>>name:%-20s     ->>>age:%-13s\n",info.name,info.age);
		printf("->>>phone:%-20s    ->>>addr:%-12s\n",info.phone,info.addr);
		printf("->>>empnum:%-20s   ->>>salary:%-10s\n",info.empnum,info.salary);
		printf("->>>level:%-20s\n",info.level);
		
		return 0;
	}else if(1 == user->type){
		if(0 > send(sockfd, user, sizeof(*user), 0)){
			perror("send");
			return -1;
		}

		if(0 > recv(sockfd, &comuni, sizeof(comuni), 0)){
			perror("recv");
			return -1;
		}

		puts("********************************************************");
		puts("*                Employee Information                  *");
		puts("********************************************************");
		
		while(comuni.choice){
			bzero(&info, sizeof(info));

			if(0 > recv(sockfd, &info, sizeof(sockfd), 0)){
				perror("recv");
				return -1;
			}
			printf("-------------------------%d------------------------\n",comuni.choice);
			printf("->>>name:%-20s    ->>>age:%-13s\n",info.name,info.age);
			printf("->>>phone:%-20s   ->>>addr:%-12s\n",info.phone,info.addr);
			printf("->>>empnum:%-20s  ->>>salary:%-10s\n",info.empnum,info.salary);
			printf("->>>level:%-20s\n",info.level);
			puts("-----------------------over---------------------------");
			putchar(10);
			comuni.choice--;
		}
	}

	return 0;
}

int modification(int sockfd, USER *user)
{


	if(0 == user->type){







	}else if(1 == user->type){








	}

	
	return 0;
}

int delete(int sockfd)
{
	USER user;
	COMUNI comuni;
	bzero(&comuni,sizeof(comuni));
	bzero(&user,sizeof(user));
	puts("please input account name of delete from table:");
	printf(".oO>> ");
	if(NULL == fgets(user.name, sizeof(user.name), stdin)){
		fprintf(stderr,"%d -> %s:fgets error\n",__LINE__,__func__);
		return -1;
	}

	user.name[strlen(user.name) - 1] = '\0';
	if(0 > send(sockfd, &user,sizeof(user), 0)){
		fprintf(stderr,"%d -> %s:send error\n",__LINE__,__func__);
		return -1;
	}

	if(0 > recv(sockfd, &comuni, sizeof(comuni), 0)){
		fprintf(stderr,"%d -> %s:recv error\n",__LINE__,__func__);
		return -1;	
	}
	if(comuni.choice == 0){
		printf("delete loginfo false,and employeeinfo false!\n");
		return -1;
	}else if(comuni.choice == 100){
		printf("account:%s doesn't exists!\n",user.name);
		return -1;
	}else if(comuni.choice == 110){
		printf("delete loginfo sucess,and delete employeeinfo false!\n");
		return -1;
	}else if(comuni.choice == 111){
		printf("*****----->>>delete account:%s success!<<<-----******\n",user.name);
	}

	return 0;
}

void quit(void)
{
	puts("quit success!");
	exit(0);
}
